//
//  MyDocument.m
//  IV8
//
//  Created by Joseph Gentle on 6/05/09.
//  Copyright __MyCompanyName__ 2009 . All rights reserved.
//

#import "Session.h"

@implementation Session

- (id)init
{
	self = [super init];
	if (self) {
		_jsContext = nil;
			// Add your subclass-specific initialization here.
			// If an error occurs here, send a [self release] message and return nil.
	}
	return self;
}

- (NSString *)windowNibName
{
    // Override returning the nib file name of the document
    // If you need to use a subclass of NSWindowController or if your document supports multiple NSWindowControllers, you should remove this method and override -makeWindowControllers instead.
    return @"MyDocument";
}

- (void)windowControllerDidLoadNib:(NSWindowController *) aController
{
	[super windowControllerDidLoadNib:aController];
}

- (NSData *)dataOfType:(NSString *)typeName error:(NSError **)outError
{
    // Insert code here to write your document to data of the specified type. If the given outError != NULL, ensure that you set *outError when returning nil.

    // You can also choose to override -fileWrapperOfType:error:, -writeToURL:ofType:error:, or -writeToURL:ofType:forSaveOperation:originalContentsURL:error: instead.

    // For applications targeted for Panther or earlier systems, you should use the deprecated API -dataRepresentationOfType:. In this case you can also choose to override -fileWrapperRepresentationOfType: or -writeToFile:ofType: instead.

    if ( outError != NULL ) {
		*outError = [NSError errorWithDomain:NSOSStatusErrorDomain code:unimpErr userInfo:NULL];
	}
	return nil;
}

- (BOOL)readFromData:(NSData *)data ofType:(NSString *)typeName error:(NSError **)outError
{
    // Insert code here to read your document from the given data of the specified type.  If the given outError != NULL, ensure that you set *outError when returning NO.

    // You can also choose to override -readFromFileWrapper:ofType:error: or -readFromURL:ofType:error: instead. 
    
    // For applications targeted for Panther or earlier systems, you should use the deprecated API -loadDataRepresentation:ofType. In this case you can also choose to override -readFromFile:ofType: or -loadFileWrapperRepresentation:ofType: instead.
    
    if ( outError != NULL ) {
		*outError = [NSError errorWithDomain:NSOSStatusErrorDomain code:unimpErr userInfo:NULL];
	}
    return YES;
}

- (JSContext *)jsContext {
	if (_jsContext == nil) {
		_jsContext = [[JSContext alloc] init];
	}
	
	return _jsContext;
}

- (IBAction)executeCommandFromField:(id)sender {
	NSString *query = [input stringValue];
	if ([query length] < 1) return;
	NSString *result = [[self jsContext] executeString:query];
	NSLog(@"'%@' -> '%@'", query, result);
	
	// There must be a better way to do this attributed string stuff...
	query = [query stringByAppendingString:@"\n"];
	result = [result stringByAppendingString:@"\n"];

	NSDictionary *attr =
		[NSDictionary
		 dictionaryWithObject:[NSFont fontWithName:@"Monaco" size:10] forKey:NSFontAttributeName];
	NSDictionary *greyAttr =
		[NSDictionary dictionaryWithObjectsAndKeys:
		 [NSFont fontWithName:@"Monaco" size:10], NSFontAttributeName,
		 [NSColor grayColor], NSForegroundColorAttributeName,
		 NULL
	];
	
	NSMutableAttributedString *output = [[NSMutableAttributedString alloc]
	                                     initWithString:@"> "
	                                         attributes:attr];

	NSAttributedString *queryAttrStr = [[NSAttributedString alloc] initWithString:query attributes:attr];
	[output appendAttributedString:queryAttrStr];
	
	NSAttributedString *resultAttrStr = [[NSAttributedString alloc] initWithString:result attributes:greyAttr];
	[output appendAttributedString:resultAttrStr];

    NSRange endRange;
    NSTextStorage *s = [textView textStorage];
	endRange.location = [s length];
    [s appendAttributedString:output];
    endRange.length = [output length];
    [textView scrollRangeToVisible:endRange];
	[input setStringValue:@""];
}

@end
